package com.sinacom.ibuilding.util;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.lang.StringUtils;
import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.sinacom.ibuilding.beans.Member;
import com.sinacom.ibuilding.service.MessageService;

/**
 * 拦截器 - 判断会员是否登录
 */

public class MemberLoginInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = -86246303854807787L;
	MessageService messageService = new MessageService();
	public String intercept(ActionInvocation invocation) throws Exception {
		Member member = (Member) invocation.getInvocationContext().getSession().get("member");
		HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
		if (member == null) {
			
			
			String redirectionUrl = request.getRequestURL().toString();
			String queryString = request.getQueryString();
			if (StringUtils.isNotEmpty(queryString)) {
				redirectionUrl += "?" + queryString;
			}
			request.getSession().setAttribute("redirectionUrl", redirectionUrl);
			return "login";
		}
		int noReadCount = messageService.getNoReadEmail(member);
		request.setAttribute("noReadCount", new Integer(noReadCount));
		return invocation.invoke();
	}

}